#ifndef MD2_MODEL_H
#define MD2_MODEL_H

#include <stdlib.h>    
#include "gl/glut.h"
#include "..\vector.h"

struct MD2Vertex
{
	Vector pos;
	Vector normal;
};

struct MD2Frame
{
	char name[16];
	MD2Vertex* vertices;
};

struct MD2TexCoord
{
	float texCoordX;
	float texCoordY;
};

struct MD2Triangle
{
	int vertices[3];  //The indices of the vertices in this triangle
	int texCoords[3]; //The indices of the texture coordinates of the triangle
};

class MD2Model
{
	private:
		int numFrames;
		MD2TexCoord* texCoords;
		MD2Triangle* triangles;
		int numTriangles;
		GLuint textureId;
		MD2Frame* frames;
		int startFrame; //The first frame of the current animation
		int endFrame;   //The last frame of the current animation
		bool isDead;
		//player dies (still not dead ;) - when the animation of dying finishes, then he's dead)
		
		int life;
		//the name of the frame to which the model has to return after he was
		//hit by the player and finished his temporary animation
		char *returnFrame;
		bool returnToPreviousFrame;
		
		 //The position in the current animation.  0 indicates the beginning of
		 //the animation, which is at the starting frame, and 1 indicates the
		 //end of the animation, which is right when the starting frame is
		 //reached again.  It always lies between 0 and 1.
		float time;

		bool finishedAnimation;

	protected:
		MD2Model();

	public:
		Vector* trianglesToCollide;
		~MD2Model();
		bool dies;
		//switches to the given animation
		void setAnimation(char* name, char *frameToReturn = NULL, bool _returnToPreviousFrame = false);
		//Advances the position in the current animation.  The entire animation
		//lasts one unit of time.
		void advance(float dt);
		//draws the current state of the animated model.
		void draw();
		MD2Triangle* getTriangles();
		int getNumTriangles();
		//the model was hit by the player
		void hit();

		bool getFinishedAnimation();
		void setFinishedAnimation(bool value);
		
		//Loads an MD2Model from the specified file.  Returns NULL if there was
		//an error loading it.
		static MD2Model* load(const char* filename);
};

#endif //MD2_MODEL_H
